
package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class ReviseMerchandiseActivity extends Activity {
    private TextView goods_name, cup_type;
    private Button reviseButton;
    private EditText mEditText;
    private String sku_id;
    private MerchandiseAdapter adapter;
    private String  price;
    private Intent mIntent;
    private ArrayList<SkuList> item=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_revisemerchandise);
        adapter=new MerchandiseAdapter(ReviseMerchandiseActivity.this);
        mIntent=getIntent();
        initView();
        doWork();
        reviseButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEditText.getText().toString())){
                    Toast.makeText(ReviseMerchandiseActivity.this,"输入的金额不能为空",Toast.LENGTH_LONG).show();
                    mEditText.setText("");
                    mEditText.requestFocus();
                    return;
                }
                final Double prices= Double.valueOf(mEditText.getText().toString());
                if (prices>20.00){
                    Toast.makeText(ReviseMerchandiseActivity.this,"输入的最大金额不能超过¥20.00",Toast.LENGTH_LONG).show();
                    mEditText.setText("");
                    mEditText.requestFocus();
                    return;
                }
                else if (prices<0.01){
                    Toast.makeText(ReviseMerchandiseActivity.this,"输入的最大金额不能超过¥0.01",Toast.LENGTH_LONG).show();
                    mEditText.setText("");
                    mEditText.requestFocus();
                    return;
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpContect httpContect = new HttpContect();
                        String rount = "api/goods/sku-update";
                        sku_id=mIntent.getStringExtra("sku_id");
                        Log.i("123124","****&^&&&&&&&&&&"+ sku_id);
                        SharedPreferences sharedPreferences = getSharedPreferences("data", 0);
                        String access_token = sharedPreferences.getString("access_token", null);
                        String data = "access_token=" + access_token +"&sku_id=" + sku_id +"&price="+prices;
                        httpContect.send(rount, data);
                    }
                }).start();
                    mIntent=new Intent(ReviseMerchandiseActivity.this,MerchandiseControlActivity.class);
                    startActivity(mIntent);
            }
        });

    }

    private void doWork() {
        price=mIntent.getStringExtra("price");
        DecimalFormat df=new DecimalFormat("0.00");
        mEditText.setHint("¥"+df.format(1));
        goods_name.setText(mIntent.getStringExtra("sku_name"));

    }


    private void initView() {
        goods_name = findViewById(R.id.tast_name);
        cup_type = findViewById(R.id.cup_type);
        reviseButton = findViewById(R.id.revise_button);
        mEditText = findViewById(R.id.price);

    }

}

